home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Game Programming Gurus / Tricks of the Windows Game Programming Gurus (SAMS)(2000).iso / Goodies / t3dlib2.cpp < prev    next >
C/C++ Source or Header  |  1999-08-27  |  10KB  |  391 lines

  1. // T3DLIB2.CPP - Game Engine Part II
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4.  
  5. #define WIN32_LEAN_AND_MEAN  
  6. // #define INITGUID
  7.  
  8. #include <windows.h>   // include important windows stuff
  9. #include <windowsx.h> 
  10. #include <mmsystem.h>
  11. #include <objbase.h>
  12. #include <iostream.h> // include important C/C++ stuff
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <malloc.h>
  16. #include <memory.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23.  
  24. #include <ddraw.h>  // directX includes
  25. #include <dinput.h>
  26. #include "T3DLIB1.H"
  27. #include "T3DLIB2.H"
  28.  
  29. // DEFINES ////////////////////////////////////////////////
  30.  
  31. // TYPES //////////////////////////////////////////////////
  32.  
  33. // PROTOTYPES /////////////////////////////////////////////
  34.  
  35. // EXTERNALS /////////////////////////////////////////////
  36.  
  37. extern HWND main_window_handle;     // access to main window handle in main module
  38. extern HINSTANCE main_instance; // save the instance
  39.  
  40. // GLOBALS ////////////////////////////////////////////////
  41.  
  42. // directinput globals
  43. LPDIRECTINPUT        lpdi      = NULL;    // dinput object
  44. LPDIRECTINPUTDEVICE  lpdikey   = NULL;    // dinput keyboard
  45. LPDIRECTINPUTDEVICE  lpdimouse = NULL;    // dinput mouse
  46. LPDIRECTINPUTDEVICE2 lpdijoy   = NULL;    // dinput joystick
  47. GUID                 joystickGUID;        // guid for main joystick
  48. char                 joyname[80];         // name of joystick
  49.  
  50. // these contain the target records for all di input packets
  51. UCHAR keyboard_state[256]; // contains keyboard state table
  52. DIMOUSESTATE mouse_state;  // contains state of mouse
  53. DIJOYSTATE joy_state;      // contains state of joystick
  54. int joystick_found = 0;    // tracks if joystick was found and inited
  55.  
  56. // FUNCTIONS //////////////////////////////////////////////
  57.  
  58. //////////////////////////////////////////////////////////////////////////////
  59.  
  60. BOOL CALLBACK DInput_Enum_Joysticks(LPCDIDEVICEINSTANCE lpddi,
  61.                                 LPVOID guid_ptr) 
  62. {
  63. // this function enumerates the joysticks, but
  64. // stops at the first one and returns the
  65. // instance guid of it, so we can create it
  66.  
  67. *(GUID*)guid_ptr = lpddi->guidInstance; 
  68.  
  69. // copy name into global
  70. strcpy(joyname, (char *)lpddi->tszProductName);
  71.  
  72. // stop enumeration after one iteration
  73. return(DIENUM_STOP);
  74.  
  75. } // end DInput_Enum_Joysticks
  76.  
  77. //////////////////////////////////////////////////////////////////////////////
  78.  
  79. int DInput_Init(void)
  80. {
  81. // this function initializes directinput
  82.  
  83. if (FAILED(DirectInputCreate(main_instance,DIRECTINPUT_VERSION,&lpdi,NULL)))
  84.    return(0);
  85.  
  86. // return success
  87. return(1);
  88.  
  89. } // end DInput_Init
  90.  
  91. ///////////////////////////////////////////////////////////
  92.  
  93. void DInput_Shutdown(void)
  94. {
  95. // this function shuts down directinput
  96.  
  97. if (lpdi)
  98.    lpdi->Release();
  99.  
  100. } // end DInput_Shutdown
  101.  
  102. ///////////////////////////////////////////////////////////
  103.  
  104. int DInput_Init_Joystick(int min_x, int max_x, int min_y, int max_y, int dead_zone)
  105. {
  106. // this function initializes the joystick, it allows you to set
  107. // the minimum and maximum x-y ranges 
  108.  
  109. // first find the fucking GUID of your particular joystick
  110. lpdi->EnumDevices(DIDEVTYPE_JOYSTICK, 
  111.                   DInput_Enum_Joysticks, 
  112.                   &joystickGUID, 
  113.                   DIEDFL_ATTACHEDONLY); 
  114.  
  115. // create a temporary IDIRECTINPUTDEVICE (1.0) interface, so we query for 2
  116. LPDIRECTINPUTDEVICE lpdijoy_temp = NULL;
  117.  
  118. if (lpdi->CreateDevice(joystickGUID, &lpdijoy_temp, NULL)!=DI_OK)
  119.    return(0);
  120.  
  121. // now that we have created the joystick device we need to
  122. // obtain a new more advanced interface called 
  123. // IDIRECTINPUTDEVICE2, this needs to be done using the low level
  124. // COM method queryinterface(), unfortunetely createdevice
  125. // still gives a 1.0 version interface, so we to do this manually
  126.  
  127. lpdijoy_temp->QueryInterface(IID_IDirectInputDevice2, 
  128.                             (void **) &lpdijoy); 
  129.  
  130. // release the version 1.0 interface since we have converted
  131. // it to 2.0
  132. lpdijoy_temp->Release();
  133.  
  134. // set cooperation level
  135. if (lpdijoy->SetCooperativeLevel(main_window_handle, 
  136.                      DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
  137.    return(0);
  138.  
  139. // set data format
  140. if (lpdijoy->SetDataFormat(&c_dfDIJoystick)!=DI_OK)
  141.    return(0);
  142.  
  143. // set the range of the joystick
  144. DIPROPRANGE joy_axis_range;
  145.  
  146. // first x axis
  147. joy_axis_range.lMin = min_x;
  148. joy_axis_range.lMax = max_x;
  149.  
  150. joy_axis_range.diph.dwSize       = sizeof(DIPROPRANGE); 
  151. joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
  152. joy_axis_range.diph.dwObj        = DIJOFS_X;
  153. joy_axis_range.diph.dwHow        = DIPH_BYOFFSET;
  154.  
  155. lpdijoy->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
  156.  
  157. // now y-axis
  158. joy_axis_range.lMin = min_y;
  159. joy_axis_range.lMax = max_y;
  160.  
  161. joy_axis_range.diph.dwSize       = sizeof(DIPROPRANGE); 
  162. joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
  163. joy_axis_range.diph.dwObj        = DIJOFS_Y;
  164. joy_axis_range.diph.dwHow        = DIPH_BYOFFSET;
  165.  
  166. lpdijoy->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
  167.  
  168.  
  169. // and now the dead band
  170. DIPROPDWORD dead_band; // here's our property word
  171.  
  172. // scale dead zone by 100
  173. dead_zone*=100;
  174.  
  175. dead_band.diph.dwSize       = sizeof(dead_band);
  176. dead_band.diph.dwHeaderSize = sizeof(dead_band.diph);
  177. dead_band.diph.dwObj        = DIJOFS_X;
  178. dead_band.diph.dwHow        = DIPH_BYOFFSET;
  179.  
  180. // deadband will be used on both sides of the range +/-
  181. dead_band.dwData            = dead_zone;
  182.  
  183. // finally set the property
  184. lpdijoy->SetProperty(DIPROP_DEADZONE,&dead_band.diph);
  185.  
  186. dead_band.diph.dwSize       = sizeof(dead_band);
  187. dead_band.diph.dwHeaderSize = sizeof(dead_band.diph);
  188. dead_band.diph.dwObj        = DIJOFS_Y;
  189. dead_band.diph.dwHow        = DIPH_BYOFFSET;
  190.  
  191. // deadband will be used on both sides of the range +/-
  192. dead_band.dwData            = dead_zone;
  193.  
  194.  
  195. // finally set the property
  196. lpdijoy->SetProperty(DIPROP_DEADZONE,&dead_band.diph);
  197.  
  198. // acquire the joystick
  199. if (lpdijoy->Acquire()!=DI_OK)
  200.    return(0);
  201.  
  202. // set found flag
  203. joystick_found = 1;
  204.  
  205. // return success
  206. return(1);
  207.  
  208. } // end DInput_Init_Joystick
  209.  
  210. ///////////////////////////////////////////////////////////
  211.  
  212. int DInput_Init_Mouse(void)
  213. {
  214. // this function intializes the mouse
  215.  
  216. // create a mouse device 
  217. if (lpdi->CreateDevice(GUID_SysMouse, &lpdimouse, NULL)!=DI_OK)
  218.    return(0);
  219.  
  220. // set cooperation level
  221. if (lpdimouse->SetCooperativeLevel(main_window_handle, 
  222.                        DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
  223.    return(0);
  224.  
  225. // set data format
  226. if (lpdimouse->SetDataFormat(&c_dfDIMouse)!=DI_OK)
  227.    return(0);
  228.  
  229. // acquire the mouse
  230. if (lpdimouse->Acquire()!=DI_OK)
  231.    return(0);
  232.  
  233. // return success
  234. return(1);
  235.  
  236. } // end DInput_Init_Mouse
  237.  
  238. ///////////////////////////////////////////////////////////
  239.  
  240. int DInput_Init_Keyboard(void)
  241. {
  242. // this function initializes the keyboard device
  243.  
  244. // create the keyboard device  
  245. if (lpdi->CreateDevice(GUID_SysKeyboard, &lpdikey, NULL)!=DI_OK)
  246.    return(0);
  247.  
  248. // set cooperation level
  249. if (lpdikey->SetCooperativeLevel(main_window_handle, 
  250.                  DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
  251.     return(0);
  252.  
  253. // set data format
  254. if (lpdikey->SetDataFormat(&c_dfDIKeyboard)!=DI_OK)
  255.    return(0);
  256.  
  257. // acquire the keyboard
  258. if (lpdikey->Acquire()!=DI_OK)
  259.    return(0);
  260.  
  261. // return success
  262. return(1);
  263.  
  264. } // end DInput_Init_Keyboard
  265.  
  266. ///////////////////////////////////////////////////////////
  267.  
  268. int DInput_Read_Joystick(void)
  269. {
  270. // this function reads the joystick state
  271.  
  272. // make sure the joystick was initialized
  273. if (!joystick_found)
  274.    return(0);
  275.  
  276. if (lpdijoy)
  277.     {
  278.     // this is needed for joysticks only    
  279.     if (lpdijoy->Poll()!=DI_OK)
  280.         return(0);
  281.  
  282.     if (lpdijoy->GetDeviceState(sizeof(DIJOYSTATE), (LPVOID)&joy_state)!=DI_OK)
  283.         return(0);
  284.     }
  285. else
  286.     {
  287.     // joystick isn't plugged in, zero out state
  288.     memset(&joy_state,0,sizeof(joy_state));
  289.  
  290.     // return error
  291.     return(0);
  292.     } // end else
  293.  
  294. // return sucess
  295. return(1);
  296.  
  297. } // end DInput_Read_Joystick
  298.  
  299. ///////////////////////////////////////////////////////////
  300.  
  301. int DInput_Read_Mouse(void)
  302. {
  303. // this function reads  the mouse state
  304.  
  305. if (lpdimouse)    
  306.     {
  307.     if (lpdimouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state)!=DI_OK)
  308.         return(0);
  309.     }
  310. else
  311.     {
  312.     // mouse isn't plugged in, zero out state
  313.     memset(&mouse_state,0,sizeof(mouse_state));
  314.  
  315.     // return error
  316.     return(0);
  317.     } // end else
  318.  
  319. // return sucess
  320. return(1);
  321.  
  322. } // end DInput_Read_Mouse
  323.  
  324. ///////////////////////////////////////////////////////////
  325.  
  326. int DInput_Read_Keyboard(void)
  327. {
  328. // this function reads the state of the keyboard
  329.  
  330. if (lpdikey)
  331.     {
  332.     if (lpdikey->GetDeviceState(256, (LPVOID)keyboard_state)!=DI_OK)
  333.        return(0);
  334.     }
  335. else
  336.     {
  337.     // keyboard isn't plugged in, zero out state
  338.     memset(keyboard_state,0,sizeof(keyboard_state));
  339.  
  340.     // return error
  341.     return(0);
  342.     } // end else
  343.  
  344. // return sucess
  345. return(1);
  346.  
  347. } // end DInput_Read_Keyboard
  348.  
  349. ///////////////////////////////////////////////////////////
  350.  
  351. void DInput_Release_Joystick(void)
  352. {
  353. // this function unacquires and releases the joystick
  354.  
  355. if (lpdijoy)
  356.     {    
  357.     lpdijoy->Unacquire();
  358.     lpdijoy->Release();
  359.     } // end if
  360.  
  361. } // end DInput_Release_Joystick
  362.  
  363. ///////////////////////////////////////////////////////////
  364.  
  365. void DInput_Release_Mouse(void)
  366. {
  367. // this function unacquires and releases the mouse
  368.  
  369. if (lpdimouse)
  370.     {    
  371.     lpdimouse->Unacquire();
  372.     lpdimouse->Release();
  373.     } // end if
  374.  
  375. } // end DInput_Release_Mouse
  376.  
  377. ///////////////////////////////////////////////////////////
  378.  
  379. void DInput_Release_Keyboard(void)
  380. {
  381. // this function unacquires and releases the keyboard
  382.  
  383. if (lpdikey)
  384.     {
  385.     lpdikey->Unacquire();
  386.     lpdikey->Release();
  387.     } // end if
  388.  
  389. } // end DInput_Release_Keyboard
  390.  
  391.